package se_ssplist

import (
	"fmt"
	"wx-api/adx-api/cache"
	"wx-api/adx-api/errmsg"
	"wx-api/adx-api/handlers"
	"wx-api/adx-api/model/m_ssplist"
	"wx-api/adx-api/services/comm"
	"wx-api/adx-api/utils"
)

func List(c *handlers.Controller) {
	id := c.GetString("id")
	name := c.GetString("name")
	flag := c.GetInt32("flag", 0)
	status := c.GetInt32("status", 1)
	pageNum, pageSize := c.GetPage()
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_ssplist.List(id, name, "", flag, status, pageNum, pageSize)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select list is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Add(c *handlers.Controller) {
	var form m_ssplist.SspList
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	if form.PvTm == "" {
		form.PvTm = "2"
	}
	form.Code = utils.RandomString(6)
	form.Password = utils.MD5(form.Password)
	form.Skey = utils.RandomString(16)
	form.Ikey = utils.RandomString(16)
	id, err := m_ssplist.Save(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("insert is error"))
		return
	}
	go cache.CacheSspListMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Edit(c *handlers.Controller) {
	var form m_ssplist.SspList
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	tmp_info, err := m_ssplist.GetByCk(form.Id.Hex())
	if err != nil || tmp_info == nil {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	if form.Password == tmp_info.Password {
		form.Password = ""
	}
	err = m_ssplist.Update(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("edit is error"))
		return
	}
	go cache.CacheSspListMapUpdate(tmp_info.Id.Hex())
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Info(c *handlers.Controller) {
	id := c.GetString("id")
	if id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("必填参数为空"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_ssplist.GetByCk(id)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Flag(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("flag", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_ssplist.GetByCk(id)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_ssplist.UpdateFalg(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("flag is error"))
		return
	}
	go cache.CacheSspListMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Stats(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("status", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_ssplist.GetByCk(id)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_ssplist.UpdateDel(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("stats is error"))
		return
	}
	go cache.CacheSspListMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func All(c *handlers.Controller) {
	data := cache.CacheSspListMapAll()
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}
